package com.lagou.edu.blog.service.impl;

import com.lagou.edu.blog.dao.ArticleDao;
import com.lagou.edu.blog.pojo.Article;
import com.lagou.edu.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleDao articleDao;

    @Override
    public Page<Article> getList(Integer pageNumber, Integer pageSize) {

        if (pageNumber == null) {
            pageNumber = 0;
        }

        if (pageSize == null) {
            pageSize = 2;
        }

        Pageable pageable = PageRequest.of(pageNumber, pageSize, Sort.by(Sort.Direction.DESC, "id"));

        Page<Article> articlePage = articleDao.findAll(pageable);

        return articlePage;
    }
}
